<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>promise</title>
</head>
<body>
  

  <script>
  // const promise = new Promise(function(resolve, reject){
  //   var bool = false;
  //   setTimeout(function(){
  //     bool = true;
  //   },1500);
  //   if (bool) {
  //     resolve(value);
  //   } else {
  //     reject(error);
  //   }
  // });
  

  // 一段时间过执行then
  /*
  function timeout(ms) {
    return new Promise((resolve,reject) => {
      setTimeout(resolve, ms, 'done');
    });
  }

  timeout(1000).then((value)=>{
    console.log(value);
  });
  */
  
  /*
  let promise = new Promise(function(resolve, reject) {
    console.log('Promise');
    resolve();
  });

  promise.then(function() {
    console.log('resolved');
  });

  console.log('HI!');
  */
  
  /*
  let promise = new Promise(function(resolve, reject){
    var bool = false
    setTimeout(function(){
      bool = true;
      resolve(bool);
    },2000);
  });
  promise.then(function(res){
    if (res) {
      console.log('yes');
    } else {
      console.log('faker');
    }
  });
  */
 
  /*
  const getJSON = function(url) {
    const promise = new Promise(function(resolve, reject){
      const handler = function() {
        if (this.readyState !== 4) {
          return;
        }

        if (this.status === 200) {
          resolve(this.response);
        } else {
          reject(new Error(this.statusText));
        }
      };
      const client = new XMLHttpRequest();
      client.open('GET', url);
      client.onreadystatechange = handler;
      client.responseType = 'json';
      client.setRequestHeader('Accept', 'application/json');
      client.send();
    });
    return promise;
  }

  getJSON('/posts.json').then(function(json) {
    console.log(json);
  }, function(error) {
    console.log(error);
  });
  */
  
  /**/
  const p1 = new Promise(function (resolve, reject) {
    setTimeout(() => reject(new Error('fail')),3000);
  });

  const p2 = new Promise(function (resolve, reject) {
    setTimeout(() => resolve(p1),1000)
  });

  p2.then(function(successTxt) {
    console.log(successTxt);
    return successTxt;
  },function(failTxt) {
    console.log(failTxt);
    return failTxt;
  }).then(function(resultTxt){
    console.log(resultTxt);
    return resultTxt;
  }).then((step)=>{
    console.log(step);
  });
  
 
  /*
  new Promise(function(resolve,reject){
    setTimeout(()=>{
      resolve(65656);
    },1000);
  }).then(function(num){
    console.log(num);
    return num;
  }).then(function(num){
    console.log(num);
  });
  */
  </script>
</body>
</html>